<%-- 
    Document   : editGoods
    Created on : Oct 28, 2011, 2:12:02 AM
    Author     : Admin
--%>

<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="manufacturerBean.ManufacturerHome"%>
<%@page import="manufacturerBean.Manufacturer"%>
<%@page import="GoodsBean.GoodsHome"%>
<%@page import="categorybean.Category"%>
<%@page import="categorybean.CategoryHome"%>
<%@page import="GoodsBean.Goods"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.HashMap"%>
<%@page import="DBConnection.ATeamObject"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Изменение параметров товара</title>
        <%  InitialContext initialContext;
            GoodsHome goodsHome;
            Goods goods;
            Object homeObject;

            CategoryHome categoryHome;
            Category category;
            Object homeObject1;

            Manufacturer manufacturer;
            ManufacturerHome manufacturerHome;
            Object homeObject2;

            ATeamObject ato = new ATeamObject();

            try {
                initialContext = new InitialContext();
                homeObject = initialContext.lookup("GoodsBean");
                goodsHome = (GoodsHome) PortableRemoteObject.narrow(homeObject,
                        GoodsHome.class);

                homeObject1 = initialContext.lookup("CategoryBean");
                categoryHome = (CategoryHome) PortableRemoteObject.narrow(homeObject1,
                        CategoryHome.class);

                homeObject2 = initialContext.lookup("ManufacturerBean");
                manufacturerHome = (ManufacturerHome) PortableRemoteObject.narrow(homeObject2,
                        ManufacturerHome.class);
        %>
    </head>
    <body background="1.jpg" bgproperties="fixed">
        <h4><a href="index.jsp">Главная</a></h4> 
        <a href="editGoods.jsp">Изменение параметров товара</a>
        <h1>Изменение параметров товара</h1>
        <% if (request.getParameter("category") != null) {
                goods = goodsHome.findByPrimaryKey(Integer.parseInt(request.getParameter("goods")));
                goods.setNameOfGoods(request.getParameter("name"));
                goods.setGoodsDescription(request.getParameter("description"));
                
                category = categoryHome.findByPrimaryKey(Integer.parseInt(request.getParameter("category")));
                goods.setCategoryId(category.getObjectId());                
                
                manufacturer = manufacturerHome.findByPrimaryKey(Integer.parseInt(request.getParameter("manufacturer")));
                goods.setManufactureId(manufacturer.getObjectId());                
                
                goods.setPrice(Integer.parseInt(request.getParameter("price")));
                goods.setNumber(Integer.parseInt(request.getParameter("value")));
                
                response.sendRedirect("index.jsp");
            } else {
                goods = goodsHome.findByPrimaryKey(Integer.parseInt(request.getParameter("goodID")));                
        %>
        <form name="editGoods" action="editGoods.jsp?">
            <p> Название товара
                <input type="text" name="name" value="<%= goods.getNameOfGoods() %>" size="25" />
            </p>
            <p> Описаник товара
                <input type="text" name="description" value="<%= goods.getGoodsDescription() %>" size="25" />
            </p>
            <p> Категория товара
                <select name="category">
                    <%  Collection allCategory = categoryHome.findAll();
                        Iterator iter = allCategory.iterator();
                        while(iter.hasNext()){
                            category = (Category)iter.next();
                            if (category.getObjectId() == goods.getCategoryId()) {
                    %>
                                <option value="<%= category.getObjectId() %>" selected> <%= category.getName() %></option>
                            <%
                            } else {
                            %>
                                <option value="<%= category.getObjectId() %>"><%= category.getName() %></option>
                    <%
                            };
                        };
                    %>
                </select>
            </p>    
            <p> Производитель
                <select name="manufacturer">
                    <%  Collection allManufacturer = manufacturerHome.findAll();
                        iter = allManufacturer.iterator();
                        while(iter.hasNext()){
                            manufacturer = (Manufacturer)iter.next();
                            if (manufacturer.getObjectId() == goods.getManufactureId()) {
                    %>
                                <option value="<%= manufacturer.getObjectId() %>" selected> <%= manufacturer.getName() %></option>
                                <%
                            } else {
                                %>
                                <option value="<%= manufacturer.getObjectId() %>"><%= manufacturer.getName() %></option>
                    <%
                            };
                        };
                    %>
                </select>
            </p>
            <p> Цена
                <input type="text" name="price" value="<%= goods.getPrice() %>" size="25" />
            </p>
            <p> Количество
                <input type="text" name="value" value="<%= goods.getNumber() %>" size="25" />
            </p>
            <p>
                <input type="submit" value="Подтвердить изменение" name="edit" />
            </p>
            <input type="hidden" name="goods" value="<%=request.getParameter("goodID")%>" />
        </form> 
        <%
            };
        %>
        <% } catch (Exception e) {
                e.printStackTrace(System.err);
            }
        %>
    </body>
</html>
